package cn.hnu.qq.client.panel;

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import org.apache.log4j.Logger;

import com.sun.awt.AWTUtilities;

import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.controller.LogonController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.LogonMessage;
import cn.hnu.qq.entity.message.LogonSuccessMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.frame.ClientFrame;
import cn.hnu.qq.frame.RegisterDialog;
import cn.hnu.qq.frame.ShowYZMDialog;
import cn.hnu.qq.global.MessageType;

/**
 * 登陆面板
 * @author jared
 *
 */
public class LogonPanel extends JPanel implements ActionListener,MouseListener,MessageListener{
	private JTextField textField;
	private JPasswordField passwordField;
	private JLabel registLabel;
	private static int logonCounter = 0;
	
	private LogonController controller = LogonController.getInstance();
	/**
	 * Create the panel.
	 */
	
	public LogonPanel() {
		init();	
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}
	

	private void init() {
		this.setOpaque(false);
		setLayout(null);
		setSize(292, 233);
		JLabel acountLabel = new JLabel("acount:");
		acountLabel.setBounds(67, 72, 54, 15);
		
		add(acountLabel);
		
		textField = new JTextField();
		textField.setBounds(131, 69, 96, 21);
		add(textField);
		textField.setColumns(15);
		
		JLabel pwdLabel = new JLabel("password:");
		pwdLabel.setBounds(67, 124, 54, 15);
		add(pwdLabel);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(131, 120, 96, 21);
		add(passwordField);
		
		JButton logonButton = new JButton("logon");
		logonButton.setBounds(28, 175, 93, 23);
		add(logonButton);
		logonButton.addActionListener(this);
		
		JButton concelButton = new JButton("concel");
		concelButton.setBounds(167, 175, 93, 23);
		add(concelButton);
		
		registLabel = new JLabel("no account?regist!!");
		registLabel.setBounds(89, 99, 118, 15);
		add(registLabel);
		registLabel.addMouseListener(this);
	}

	
	
	@Override
	protected void paintComponent(Graphics g) {
		Image image = null;
		try {
			image = ImageIO.read(new File("src/main/java/image/login_right.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
				
		g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

	}


	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("logon")){
			if(logonCounter < 2){
				handleLogon();
			}else{
				ShowYZMDialog yzm = new ShowYZMDialog(controller.getClient(),true);
				handleLogon();
			}
		}	
	}

	private void handleLogon() {
		User user = new User();
		user.setAcount(textField.getText());
		user.setPassword(new String(passwordField.getPassword()));
		controller.logon(user);
	}

	public LogonController getController() {
		return controller;
	}

	public void setController(LogonController controller) {
		this.controller = controller;
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getSource() == (registLabel)){
			RegisterDialog dialog =  new RegisterDialog(controller.getClient(), true);
			Logger.getLogger(this.getClass()).debug("register Dialog 添加监听器");
			ListenServerMessageThread.getInstance().addMessageListener(dialog);//添加服务器消息监听
			dialog.setVisible(true);
		}
		
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
		if(e.getSource() == (registLabel)){
			registLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
	}

	public void mouseExited(MouseEvent e) {
	}


	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		if(message.getMessageType() == MessageType.LOGON_SUCCESS){
			logonCounter = 0;
			LogonSuccessMessage lsm = (LogonSuccessMessage) message;
			controller.setCurUser(lsm.getUser());
			new FriendsListPanel();
		}
		if(message.getMessageType() == MessageType.ACCOUNT_NOT_FOUND){
			JOptionPane.showMessageDialog(this, "账号不存在");
			logonCounter ++;
			return;
		}
		if(message.getMessageType() == MessageType.PASSWORD_FALSE){
			JOptionPane.showMessageDialog(this, "密码错误");
			logonCounter ++;
			return;
		}
	}
	
	
}
